#include <stdio.h>
#include <time.h>

#define MASK_HOUR 0x1F        // 0001 1111 (5 bits for hours)
#define MASK_MINUTE 0x7E0     // 0111 1110 0000 (7 bits for minutes, starting at bit 5)
#define MASK_SECOND 0x7F800   // 0111 1111 1000 0000 0000 (7 bits for seconds, starting at bit 11)
#define SHIFT_HOUR 0
#define SHIFT_MINUTE 5
#define SHIFT_SECOND 11

unsigned int TIME = 0;

void setHour(int h) {
    TIME &= ~MASK_HOUR;  // Clear current hour bits
    TIME |= (h & MASK_HOUR) << SHIFT_HOUR;  // Set new hour bits
}

void setMinute(int m) {
    TIME &= ~MASK_MINUTE;  // Clear current minute bits
    TIME |= (m & (MASK_MINUTE >> SHIFT_MINUTE)) << SHIFT_MINUTE;  // Set new minute bits
}

void setSecond(int s) {
    TIME &= ~MASK_SECOND;  // Clear current second bits
    TIME |= (s & (MASK_SECOND >> SHIFT_SECOND)) << SHIFT_SECOND;  // Set new second bits
}

int getHour() {
    return (TIME & MASK_HOUR) >> SHIFT_HOUR;
}

int getMinute() {
    return (TIME & MASK_MINUTE) >> SHIFT_MINUTE;
}

int getSecond() {
    return (TIME & MASK_SECOND) >> SHIFT_SECOND;
}

int main() {
    time_t rawtime;
    struct tm * timeinfo;

    time(&rawtime);  // Get the current time
    timeinfo = localtime(&rawtime);  // Convert to local time

    // Set the time using bit manipulation functions
    setHour(timeinfo->tm_hour);
    setMinute(19);
    setSecond(timeinfo->tm_sec);

    // Print the set TIME
    printf("Current TIME is: 0x%X\n", TIME);

    // Read and print the time
    printf("Current time is: %02d:%02d:%02d\n", getHour(), getMinute(), getSecond());

    return 0;
}

